package com.ala4.oxcafe.oauth;

import com.ala4.oxcafe.boot.enums.UserTypeEnum;
import com.ala4.oxcafe.boot.exception.MoreUserException;
import com.ala4.oxcafe.boot.manager.CustomUserDetails;
import com.ala4.oxcafe.boot.userdetails.PasswordUserDetailsService;
import com.ala4.oxcafe.common.utils.collection.CollUtil;
import com.ala4.oxcafe.entity.RoleInfo;
import com.ala4.oxcafe.entity.UserInfo;
import com.ala4.oxcafe.repository.UserInfoRepository;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.factory.PasswordEncoderFactories;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * 用户密码登录
 *
 * @author PING
 * @version 1.0.0
 * @date 2025/8/12 09:32
 */
@Slf4j
@Service
@AllArgsConstructor
public class PasswordUserDetailsServiceImpl implements PasswordUserDetailsService {

    private final UserInfoRepository userInfoRepository;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        log.info("PasswordUserDetailsServiceImpl-loadUserByUsername {}", username);

        List<UserInfo> userInfos = userInfoRepository.userLogin(username);

        // 如果找不到用户必须抛出 UsernameNotFoundException异常
        if (Objects.isNull(userInfos) || userInfos.isEmpty()) {
            throw new UsernameNotFoundException(username);
        }

        if (CollUtil.isNotEmpty(userInfos) && userInfos.size() > 1) {
            throw new MoreUserException("系统出现错误-有相同账号,请联系管理员");
        }

        UserInfo userInfo = userInfos.get(0);
        // 查找用户角色列表
        List<RoleInfo> roleInfos = userInfoRepository.selectUserRole(userInfo.getId());
        List<String> rolesKey = roleInfos.stream().map(RoleInfo::getRoleCode).toList();

        CustomUserDetails customUserDetails = new CustomUserDetails();
        customUserDetails.setPassword(userInfo.getPassword());
        customUserDetails.setUserAccount(userInfo.getUserAccount());
        customUserDetails.setUserType(UserTypeEnum.SYSTEM);
        customUserDetails.setPhone(userInfo.getPhoneNumber());
        customUserDetails.setNickName(userInfo.getNickName());
        customUserDetails.setUserId(userInfo.getId());
        customUserDetails.setAvatar(userInfo.getAvatar());
        customUserDetails.setRoles(rolesKey);
        customUserDetails.setStatus(userInfo.getStatus());
        customUserDetails.setIsStoreUser(userInfo.getIsStoreUser());
        customUserDetails.setStoreId(userInfo.getStoreId());

        return customUserDetails;
    }

    public static void main(String[] args) {
        PasswordEncoder delegatingPasswordEncoder = PasswordEncoderFactories.createDelegatingPasswordEncoder();
        log.info("{}", delegatingPasswordEncoder.encode("123456"));
    }
}
